﻿using JMBox.Util;
using JMBox.Utils;
using JMLibrary.Codes;

using static JMBox.Config.R;

namespace JMBox.Task
{
    public class AutoLockTask
    {
        //任务运行标志
        public static bool RunTask = false;

        //当前选择的图片
        public static string NowImgPath = null;

        //目标图片集合
        public static List<string> TargetImgPaths = new List<string>();

        public static bool AUTO_LEFT = false;

        public static bool AUTO_RIGHT = false;

        public static int AUTO_LOCK_SPEED = 10;


        /// <summary>
        /// 工作内容
        /// </summary>
        private void InnerRun()
        {
            while (RunTask && GlobalStatus.AllFunOpenFlag && GlobalStatus.AppIsRuning)
            {
                Location loc = findTargetImgUseLoc(0);

                if (loc.X != -1 && loc.Y != 1)
                {
                    if (AUTO_LEFT)
                    {
                       for(int i= 0; i < 3; i++)
                        {
                            MouseEvent mouseEventD = new MouseEvent(MouseEventType.MouseLeftDown, loc.X, loc.Y, 0);
                            ImgPoiTool.mouseHook.MouseWork(mouseEventD);
                            Thread.Sleep(10);
                            MouseEvent mouseEventU = new MouseEvent(MouseEventType.MouseLeftUp, loc.X, loc.Y, 0);
                            ImgPoiTool.mouseHook.MouseWork(mouseEventU);
                        }
                    }
                    if (AUTO_RIGHT)
                    {
                        MouseEvent mouseEventD = new MouseEvent(MouseEventType.MouseRightDown, loc.X, loc.Y, 0);
                        ImgPoiTool.mouseHook.MouseWork(mouseEventD);
                        MouseEvent mouseEventU = new MouseEvent(MouseEventType.MouseRightUp, loc.X, loc.Y, 0);
                        ImgPoiTool.mouseHook.MouseWork(mouseEventU);
                    }
                }

                Thread.Sleep(AUTO_LOCK_SPEED);
            }
        }

        /// <summary>
        /// 递归获得可用坐标
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public Location findTargetImgUseLoc(int i)
        {
            Location loc = ImgPoiTool.LocateNearestAndMove(TargetImgPaths[i]);
            //如果loc返回的是-1坐标，且TargetImgPaths不是最后一个，则继续遍历
            if (loc.X == -1 || loc.Y == -1)
            {
                if (i < (TargetImgPaths.Count - 1))
                {
                    i++;
                    return findTargetImgUseLoc(i);
                }

            }
            return loc;
        }

        /// <summary>
        /// 开始执行任务
        /// </summary>
        public void Start()
        {
            if (String.IsNullOrEmpty(TargetImgPaths[0]))
            {
                MessageBox.Show("请选择一张图片");
                return;
            }
            if (!Directory.Exists(Contact.ScreenImgDirPath))
            {
                Directory.CreateDirectory(Contact.ScreenImgDirPath);
            }

            if (AutoLockTask.RunTask && GlobalStatus.OpenAutoLock)
            {
                Thread t = new Thread(InnerRun);
                t.Start();
            }

        }

        /// <summary>
        /// 结束任务
        /// </summary>
        public void End()
        {
            RunTask = false;
        }
    }
}